import re
from packages.ply import lex

class lexer(object):
    def __init__(self):
        self.tokens = (
                "COMMENT" ,
                "INCDIR"  ,
                "LIBEXT"  ,
                "LIBFILE" ,
                "LIBDIR"  ,
                "RTLFILE" ,
                "RTLLIST"
                )
        self.t_ignore = " \t\n"

    def build(self,**kwargs):
        self.lexer = lex.lex(object=self,**kwargs)

    def token(self):
        return self.lexer.token()

    def input(self,data):
        self.lexer.input(data)

    def t_error(self,t):
        self.lexer.skip(1)

    def t_COMMENT(self,t):
        r"//.*"
        pass

    def t_INCDIR(self,t):
        r"\+incdir\+\S+"
        t.value = t.value[8:]
        return t

    def t_LIBEXT(self,t):
        r"\+libext(\+\.v\b|\+\.vp|\+\.sv)+"
        t.value = t.value[8:]
        return t

    def t_LIBFILE(self,t):
        r"-v\s+\S+"
        t.value = re.sub("-v\s+","",t.value)
        return t

    def t_LIBDIR(self,t):
        r"-y\s+\S+"
        t.value = re.sub("-y\s+","",t.value)
        return t

    def t_RTLFILE(self,t):
        r"\S+\.v\b"
        return t

    def t_RTLLIST(self,t):
        r"-f\s+\S+"
        t.value = re.sub("-f\s+","",t.value)
        return t

